import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class XLSmanager {
	

	HSSFWorkbook wb;
    HSSFSheet sheet;
	String path = new String("");
    
	public XLSmanager ( ) {
		//init();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new XLSmanager();
	}
	
	public void fileName(String name) {
		wb = new HSSFWorkbook();
		sheet = wb.createSheet("data sheet");
		File plik = new File(name);
		//plik.delete();
		try { 
			plik.mkdirs();
			path = (plik.getAbsoluteFile()+"\\data.xls");
		} catch (Exception e) {System.out.println("|fileName|"+e);}
	}
	
	public void setPath(String arg) {
		try {
			POIFSFileSystem fs =  new POIFSFileSystem(new FileInputStream(new File(arg)));
			System.out.println("Data file: "+arg +" created");
			wb = new HSSFWorkbook(fs);
			sheet = wb.getSheet("data sheet");
		} catch (Exception e) {System.out.println(e);}
		path = arg;
	}
	
	public void DataRelay() {

		    // Create a row and put some cells in it. Rows are 0 based.
		    HSSFRow row = sheet.createRow((short)0);
		    // Create a cell and put a value in it.
		    HSSFCell cell = row.createCell((short)0);
		    cell.setCellValue(1);

		    // Or do it on one line.
		    row.createCell((short)1).setCellValue(1.2);
		    row.createCell((short)2).setCellValue("This is a string");
		    row.createCell((short)3).setCellValue(true);

		    // Write the output to a file
		    try {
		    FileOutputStream fileOut = new FileOutputStream(path);
		    wb.write(fileOut);
		    fileOut.close();
		    } catch (Exception e) {
		    	System.out.println(e);
		    }

	}
	
	public void addData (int y,int x, long data) {
		HSSFRow row = sheet.createRow((short)y);
		HSSFCell cell = row.createCell((short)x);
	    cell.setCellValue(data);
	    try {
		    FileOutputStream fileOut = new FileOutputStream(path);
		    wb.write(fileOut);
		    fileOut.close();
		    } catch (Exception e) {
		    	System.out.println("|addData| "+e);
		    }
	}
	
	public String getData (int y,int x, File file) {
		try {
			POIFSFileSystem fs =  new POIFSFileSystem(new FileInputStream(file));
			HSSFWorkbook wb2 = new HSSFWorkbook(fs);
			HSSFSheet ashe = wb2.getSheet("data sheet");
			HSSFRow row = ashe.getRow(y);
			HSSFCell cell = row.getCell((short)x);
			return ""+(long)cell.getNumericCellValue();
		} catch (Exception e) {
			
		}
		return "";
 	}

}
